<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>生成器</title>
</head>
<body>
  <script>
    /* 
      生成器其实就是一个特殊的函数
      异步编程  纯回调函数  node fs ajax mongodb
      函数代码的分隔符

      生成器(Generator)是一个能直接创建并返回迭代器的特殊函数，可将其赋给可迭代对象的Symbol.iterator属性
    */
    
    /* 
    function * gen() {
      console.log("111");
      yield '一只没有耳朵';

      console.log("222");
      yield '一只没有尾巴';

      console.log("333");
      yield '真奇怪';

      console.log("444");
    }
    let iterator = gen();
    // console.log(iterator);

    iterator.next();  // 111
    iterator.next();  // 222
    iterator.next();  // 333
    iterator.next();  // 444 
    */
   
    function * gen() {
      yield '一只没有耳朵';
      yield '一只没有尾巴';
      yield '真奇怪';
    }

    let iterator = gen();
    console.log(iterator.next());   // {value: "一只没有耳朵", done: false}
    console.log(iterator.next());   // {value: "一只没有尾巴", done: false}
    console.log(iterator.next());   // {value: "真奇怪", done: false}
    console.log(iterator.next());   // {value: undefined, done: true}

    for(let v of gen()) {
      console.log(v)
    }

    function * generator() {
      var count = 0
      while (count < 2) {
        yield count++
      }

      return count
    }

    var iterator1 = generator()
    console.log(iterator1.next())
    console.log(iterator1.next())
    console.log(iterator1.next())

  </script>
</body>
</html>